package com.example.mvicore.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

abstract class BaseFragment : Fragment(){

    private var isMHidden:Boolean = false
    private var isLoaded:Boolean = false
    private var isCreated:Boolean = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initEvent()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        this.isMHidden = hidden
    }

    /**
     * 初始化视图
     */
    protected open fun initView(view : View){}

    /**
     * 初始化数据
     */
    private fun initData(){
        if (userVisibleHint &&!this.isMHidden&&!this.isLoaded&&this.isCreated ){
            lazyLoadData()
        }
        this.isLoaded = true
    }

    /**
     * 懒加载数据
     */
    open fun lazyLoadData(){}

    /**
     * 初始化事件
     */
    protected open fun initEvent(){}

    abstract fun generateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ):View

    /**
     * 获取我们基础框架里面的BaseActivity 主要目的是复用里面的对应如 调转 提示等功能
     */
    fun getOwnActivity():BaseActivity{
        return this.requireActivity() as BaseActivity
    }

}